/*
 * @Author: your name
 * @Date: 2024-05-14 10:54:09
 * @LastEditTime: 2024-05-14 11:12:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\47.全排列-ii.cpp
 */
/*
 * @lc app=leetcode.cn id=47 lang=cpp
 *
 * [47] 全排列 II
 */

// @lc code=start
class Solution {
public:
    vector<int>path;
    vector<vector<int>> res;
    void backtracking(vector<int>& nums, vector<bool>& used){
        if(path.size() == nums.size()){
            res.push_back(path);
            return;
        }

        for(int i = 0; i < nums.size(); i++){
            if( i > 0 && nums[i-1] == nums[i] && used[i-1] == 0){
                continue;
            }
            if(used[i] == 0){// 本层没用过的
                used[i] = 1;
                path.push_back(nums[i]);
                backtracking(nums, used);
                path.pop_back();
                used[i] = 0;
            }

        }
    }
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<bool> used(nums.size(), false);
        sort(nums.begin(), nums.end());
        backtracking(nums, used);
        return res;
    }
};
// @lc code=end

